﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Collections;

namespace VTNT.Framework.Web.Global
{
    public enum JsonType
    {
        String = 0,
        Boolean = 1,
        Array = 2
    }
    public class CJsonItem
    {
        public CJsonItem(string name, string value)
        {
            Name = name;
            Value = value;
            Type = JsonType.String;
        }
        public CJsonItem(string name, string value, JsonType type)
        {
            Name = name;
            Value = value;
            Type = type;
        }

        public string Name { get; set; }
        public string Value { get; set; }
        public JsonType Type { get; set; }

        public override string ToString()
        {
            string ret = "";
            string template = "{0} : \"{1}\"";
            if (Type == JsonType.Array || Type == JsonType.Boolean)
                template = "{0} : {1}";

            ret = string.Format(template, Name, Value);
            return ret;
        }
    }

    public class CJson : CollectionBase
    {
        public string ForJSON(string aText)
        {
            try
            {
                if (string.IsNullOrEmpty(aText))
                {
                    return string.Empty;
                }
                StringBuilder result = new StringBuilder();
                for (int i = 0; i < aText.Length; i++)
                {
                    char character = aText[i];
                    switch (character)
                    {
                        case '\"':
                            result.Append("\\\"");
                            break;
                        case '\'':
                            result.Append("\\\'");
                            break;
                        default:
                            result.Append(character);
                            break;
                    }
                }
                return result.ToString();
            }
            catch (Exception ex)
            {
                //CLogManager.WritePL("Mods||Global||CJson.cs|ForJSON(string)", ex.Message);
                return string.Empty;
            }
        }

        public CJson() { }
        public CJson(string description, string variableName, bool addVarKeyword)
        {
            Description = description;
            VariableName = variableName;
            AddVarKeyword = addVarKeyword;
        }

        public string Description { get; set; }
        public string VariableName { get; set; }
        public bool AddVarKeyword { get; set; }
        public void AddVariable(string name, string value, JsonType type)
        {
            this.InnerList.Add(new CJsonItem(name, value, type));
        }
        public void AddVariable(string name, string value)
        {
            this.InnerList.Add(new CJsonItem(name, value));
        }
        public CJsonItem GetItem(int index)
        {
            return (CJsonItem)this.InnerList[index];
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("\n//JSON::{0}\n", Description);
            if (AddVarKeyword)
                sb.AppendFormat("var {0} = {{\n", VariableName);
            else
                sb.AppendFormat("{0} = {{\n", VariableName);
            for (int i = 0; i < this.InnerList.Count; i++)
            {
                sb.AppendFormat("\t{0},\n", GetItem(i).ToString());
            }

            sb.Replace(",", "", sb.Length - 2, 2);
            sb.Append("};\n");
            return sb.ToString();
        }
    }
}
